package jp.ac.aiit.spg.web.status;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.EJB;

import org.seasar.teeda.extension.annotation.scope.SubapplicationScope;

import jp.ac.aiit.spg.entity.SpgProject;
import jp.ac.aiit.spg.service.SpgService;
import jp.ac.aiit.spg.web.login.LoginPage;

public class SelectProjectsPage extends AbstractStatusPage {

	@EJB
	private SpgService ss;
	
	private Collection<SpgProject> projects;
	
	private List<SelectProjectItem> selectprjItems;
	
	@SubapplicationScope
	private int projectid;
	
	private String projectName;
	
	private String projectDescription;
	
	public Class initialize() {
		//ここにログインチェックを入れる
		if (this.isLoggedIn() == false) {
			return LoginPage.class;
		} else {
			return null;			
		}
	}
	
	public Class prerender() {

//		これはサンプル用
//		prjsItems = new ArrayList<Map>();
//		Map map1 = new HashMap();
//		map1.put("label", "PBL0701");
//		map1.put("value", new Integer(1));
//		prjsItems.add(map1);
//		Map map2 = new HashMap();
//		map2.put("label", "PBL0704");
//		map2.put("value", new Integer(2));
//		prjsItems.add(map2);
//		Map map3 = new HashMap();
//		map3.put("label", "PBL0708");
//		map3.put("value", new Integer(3));
//		prjsItems.add(map3);
		
		doSelectProject();
		return null;
	}

	private void doSelectProject() {
		selectprjItems = new ArrayList<SelectProjectItem>();
		//取り急ぎすべてのプロジェクトを表示する
		//List<SpgProject> projects = ss.listAllProject();
		List<SpgProject> projects = ss.listUserProject(this.getLoginUserId());
		
		for(SpgProject p : projects) {
			SelectProjectItem item = new SelectProjectItem();
			item.setProjectId(p.getId());
			item.setProjectName(p.getName());
			item.setProjectDescription(p.getDescription());
			selectprjItems.add(item);
		}
	}

	public Collection<SpgProject> getProjects() {
		return projects;
	}

	public void setProjects(Collection<SpgProject> projects) {
		this.projects = projects;
	}

	public List<SelectProjectItem> getSelectprjItems() {
		return selectprjItems;
	}

	public void setSelectprjItems(List<SelectProjectItem> selectprjItems) {
		this.selectprjItems = selectprjItems;
	}

	public int getProjectid() {
		return projectid;
	}

	public void setProjectid(int projectid) {
		this.projectid = projectid;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getProjectDescription() {
		return projectDescription;
	}

	public void setProjectDescription(String projectDescription) {
		this.projectDescription = projectDescription;
	}
}
